<?php
class frbCms_MainController extends DooController
{
  private $name = 'test';
  private $col = 1;

  /**
   *
   * Retourne le widget du module
   * @return Array [name]=>[contenu] contenu du widget
   */
  public function getWidget() {

    echo '<h1>Gestion de contenu</h1><p>Permet de gérer les textes du site</p>';

  }

  /**
   *
   * Retourne les propriétés du widget Son nom et son nombre de colonne
   * @return Array [name]=>[nbColonne]
   */
  public function getWidgetProperties() {
    echo $this->col;
  }

  public function getWidgetTitle() {
    echo "CMS";
  }

  public function getMenu() {
    echo "CMS";
  }

  public function index() {
    Doo::loadModel('FrbcmsContent', 'frbCms');
    if ($this->isAjax()) {
      if ($this->isPost()) {
        if (!empty($_POST['id_content']) && !empty($_POST['content']) && is_numeric($_POST['id_content'])) {
          $content = new FrbcmsContent();
          $content->cnt_id = $_POST['id_content'];
          $content->cnt_content = stripslashes($_POST['content']);
          $this->db()->update($content);
          echo "true";
          return;
        }
      }
      echo "false";
      return;
    }
    Doo::loadModel('FrbcmsPage');
    $pages = new FrbcmsPage();
    $data['menu'] = $this->db()->find($pages);
    $data['baseurl'] = Doo::conf()->APP_URL;
    if (!empty($this->params[0])) {
      $pages->pge_name = $this->params[0];
      $page = $this->db()->find($pages, array('limit'=>'1'));
      if (!empty($page)) {
        $contents = new FrbcmsContent();
        $contents->pge_id = $page[0]->pge_id;
        //Pour récupérer les contents de type HTML !!
        $contents->tpe_id = 1;
        $data['contents'] = $this->db()->find($contents);
        $this->view()->render('editContent', $data);
      }
      return;
    }
    $this->view()->render('index', $data);
  }

  private function isPost() {
    if ($_SERVER['REQUEST_METHOD'] == "POST") return true;
    else return false;
  }

}